

import '../util/cache_util.dart';
import 'GlobalConfig.dart';

class ApiConfig {

  //表示配置渠道名字 flutter run --deart-define=DEFINE_APP_ENV=changqing
  static const String envChannelKey="DEFINE_APP_ENV";

  static const String appEnv=String.fromEnvironment(envChannelKey);

  ///上传文件
  static const String uploadURL = "/rest/2.0/image-classify/v1/body_seg";

  ///表示租户ID
  static String getTenantid = ""; //
  ///用户编码
  static String get userCode => GlobalConfig.loginEntity?.userCode ?? ""; //
  ///用户ID
  static String get userId => (GlobalConfig.loginEntity?.userId ?? "").toString(); //
  ///用户名称
  static String get userName => GlobalConfig.loginEntity?.userName ?? ""; //
  ///表示token
  static String get getToken {
    // 先从内存里取
    if (GlobalConfig.loginEntity?.token?.isNotEmpty == true) {
      return GlobalConfig.loginEntity?.token ?? "";
    }
    // 内存里没有就去cache里取
    Map<String, dynamic>? map = CacheUtil().getJson(GlobalConfig.LoginJson);
    if (map != null && map["token"] != null) {
      return map["token"];
    }
    // cache里也没有就返回一个默认的
    return "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjEsImRldmljZSI6ImRlZmF1bHQtZGV2aWNlIiwiZWZmIjotMSwicm5TdHIiOiJrdEswU2NqR2xqajlIN0hOSFZqZ2hkMXdVSWFHZVZYRCIsInVzZXJuYW1lIjoi5p2O6L6JIiwicGhvbmVOdW1iZXIiOiIxNTU1NjUzMTk5MyIsInRlbmFudElkIjoyMDIyMDYxNH0.cfmWq4gI-LbB6_Y9bVZEriJNHbTJpBeWno8mBdApS6Q";
  }

  ///主地址
  static List<Map<String, String>> listNetWork = [
    {"code": "pro", "name": "生产环境"},
    // {"code": "pre", "name": "预发布环境"},
    {"code": "test", "name": "测试环境"},
    {"code": "dev", "name": "开发环境"}
  ];
  static String get currentNetWork{
    String? temp=CacheUtil().get("cacheNetWork");
    if(temp?.isNotEmpty==true){
      return temp!;
    }
    return "pro"; //表示生产环境
  }

  static String get mainUrl {
    String? u = CacheUtil().get("mainUrl");
    if (u?.isNotEmpty == true) {
      return u!;
    }
    String url;
   if(appEnv==""){
     url=getDefaultUrl(currentNetWork);
   }else{
     //表示根据渠道名字获取地址
     url=getUrl(appEnv,currentNetWork);
   }

    return url;
  }
  static String getDefaultUrl(currentNetWork){
    var url;
    switch (currentNetWork) {
      case "pro":
        url = "http://36.33.42.190:1080";
        break;
      case "test":
        url = "http://36.33.42.190:1080";
        break;
      case "dev":
        url = "http://36.33.42.190:1080";
        break;
      default:
        url = "http://36.33.42.190:1080";
        break;
    }
    return url;
  }

  static List listServe=[{"procode":"changqing","pro":"10.189.100.118:18080"}];

  static String  getUrl(envName,currenNetWork){
    String url="";
    listServe.forEach((element) {
      if(element["procode"]==envName){
        if(url==""){
          url=element[currenNetWork]??element["defalut"];
        }
      }
    });
    return url!=""?url:getDefaultUrl(currentNetWork);
  }

  static String baiduApi = "https://aip.baidubce.com/oauth/2.0/token?";
  static String baiduOcrUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=";
  /// 登录地址
  static String login = "/user/do/login";
  static String uploadFile = "/upload/file";
  static String customerInfo = "/customer/save/customer/info";
  static String customerPage = "/customer/page";
  static String reconnaissance = "/reconnaissance/save/reconnaissance";
  static String reconnaissancePage = "/reconnaissance/page";
  static String customerDetail = "/customer/detail";
  static String constructionPage= "/construction/page";
  static String gridPage= "/grid/page";
  static String saveGridPage= "/grid/save/grid/connected";
  static String saveConstruction= "/construction/save/Construction";
  static String approvalUrl= "/started/save/started/approval";
  static String saveOperationUrl= "/maintenance/save/operation/maintenance";
  static String queryOperationUrl= "/maintenance/page";
  static String operationDetailUrl= "/maintenance/detail";
  static String queryCustomerUrl= "/maintenance/customer/info/page";
  static String stationStatus= "/open/station/list/status";
  static String stationList= "/open/station/list/info";
  static String elecChart= "/open/elec/chart";
  static String stationElecChart= "/open/protocol/station/statistic/elec/chart";
}
